<?php
/*************************************************

验证码发送公共控制器 - the 
Author: WangHuabin <17-5-26>

*************************************************/
namespace Home\Controller;
use Think\Controller;
class SendController extends Controller {
 function sendsms($phone, $code, $type = 1) {
                $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
                $smsConf = array(
                    'key'   => C('SMG_KEY'), //您申请的APPKEY
                    'mobile'    =>$phone, //接受短信的用户手机号码
                    'tpl_id'    => '29978', //您申请的短信模板ID，根据实际情况修改
                    'tpl_value' =>'#code#='.$code //您设置的模板变量，根据实际情况修改
                );
                $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信

                if($content){
                    $result = json_decode($content,true);
                    $error_code = $result['error_code'];
                    if($error_code == 0){
                        //状态为0，说明短信发送成功
                        // echo "短信发送成功,短信ID：".$result['result']['sid'];
                        return array('errorCode'=>1,'msg'=>"短信发送成功,短信ID：".$result['result']['sid']);
                    }else{
                        //状态非0，说明失败
                        $msg = $result['reason'];
                      //  echo "短信发送失败(".$error_code.")：".$msg;
                         return array('errorCode'=>0,'msg'=>"短信发送失败(".$error_code.")：".$msg);
                    }
                }else{
                    //返回内容异常，以下可根据业务逻辑自行修改
                  //  echo "请求发送短信失败";
                    return array('errorCode'=>0,'msg'=>"请求发送短信失败");
                }
    }
    //用于申请通知
     function sendmsg($phone, $tel, $name) {
      //  return $tel;die;
                $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
                $smsConf = array(
                    'key'   => '4b893b842f2a4dc646566aeaa8f8a5ad', //您申请的APPKEY
                    'mobile'    =>$phone, //接受短信的用户手机号码
                    'tpl_id'    => '29980', //您申请的短信模板ID，根据实际情况修改
                    'tpl_value' =>'#code#='.$tel.'&#name#='.$name //您设置的模板变量，根据实际情况修改
                );
                $content = juhecurl($sendUrl,$smsConf,1); //请求发送短信

                if($content){
                    $result = json_decode($content,true);
                    $error_code = $result['error_code'];
                    if($error_code == 0){
                        //状态为0，说明短信发送成功
                        // echo "短信发送成功,短信ID：".$result['result']['sid'];
                        return array('errorCode'=>1,'msg'=>"短信发送成功,短信ID：".$result['result']['sid']);
                    }else{
                        //状态非0，说明失败
                        $msg = $result['reason'];
                      //  echo "短信发送失败(".$error_code.")：".$msg;
                         return array('errorCode'=>0,'msg'=>"短信发送失败(".$error_code.")：".$msg);
                    }
                }else{
                    //返回内容异常，以下可根据业务逻辑自行修改
                  //  echo "请求发送短信失败";
                    return array('errorCode'=>0,'msg'=>"请求发送短信失败");
                }
    }
  //用户提现发起
    function cash_apply($phone, $date,$money,$deduction) {
      //  return $tel;die;
                $sendUrl = 'http://v.juhe.cn/sms/send'; //短信接口的URL
                $smsConf = array(
                    'key'   => '4b893b842f2a4dc646566aeaa8f8a5ad', //您申请的APPKEY
                    'mobile'    =>$phone, //接受短信的用户手机号码
                    'tpl_id'    => '46017', //您申请的短信模板ID，根据实际情况修改
                    'tpl_value' =>'#code#='.$date.'&#code2#='.$money.'&#code3#='.$deduction //您设置的模板变量，根据实际情况修改
                );
                $content = $this->juhecurl($sendUrl,$smsConf,1); //请求发送短信

                if($content){
                    $result = json_decode($content,true);
                    $error_code = $result['error_code'];
                    if($error_code == 0){
                        //状态为0，说明短信发送成功
                        // echo "短信发送成功,短信ID：".$result['result']['sid'];
                        return array('errorCode'=>1,'msg'=>"短信发送成功,短信ID：".$result['result']['sid']);
                    }else{
                        //状态非0，说明失败
                        $msg = $result['reason'];
                      //  echo "短信发送失败(".$error_code.")：".$msg;
                         return array('errorCode'=>0,'msg'=>"短信发送失败(".$error_code.")：".$msg);
                    }
                }else{
                    //返回内容异常，以下可根据业务逻辑自行修改
                  //  echo "请求发送短信失败";
                    return array('errorCode'=>0,'msg'=>"请求发送短信失败");
                }
    }

  function juhecurl($url,$params=false,$ispost=0){
            $httpInfo = array();
            $ch = curl_init();
            curl_setopt( $ch, CURLOPT_HTTP_VERSION , CURL_HTTP_VERSION_1_1 );
            curl_setopt( $ch, CURLOPT_USERAGENT , 'Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.22 (KHTML, like Gecko) Chrome/25.0.1364.172 Safari/537.22' );
            curl_setopt( $ch, CURLOPT_CONNECTTIMEOUT , 30 );
            curl_setopt( $ch, CURLOPT_TIMEOUT , 30);
            curl_setopt( $ch, CURLOPT_RETURNTRANSFER , true );
            if( $ispost )
            {
                curl_setopt( $ch , CURLOPT_POST , true );
                curl_setopt( $ch , CURLOPT_POSTFIELDS , $params );
                curl_setopt( $ch , CURLOPT_URL , $url );
            }
            else
            {
                if($params){
                    curl_setopt( $ch , CURLOPT_URL , $url.'?'.$params );
                }else{
                    curl_setopt( $ch , CURLOPT_URL , $url);
                }
            }
            $response = curl_exec( $ch );
            if ($response === FALSE) {
                //echo "cURL Error: " . curl_error($ch);
                return false;
            }
            $httpCode = curl_getinfo( $ch , CURLINFO_HTTP_CODE );
            $httpInfo = array_merge( $httpInfo , curl_getinfo( $ch ) );
            curl_close( $ch );
            return $response;
        }

}